import {
	BASE_URL
} from '@/env.js'
import {
	getToken,
	setToken
} from './token.js'
import store from '@/store/index.js'
import {
	getUserInfo,
	getUserToken,
	setUserInfo
} from "./storage.js"
export const myRequest = (options) => { //传入的options是一个json对象
	return new Promise((resolve, reject) => {
		if (!getToken('isLogin') || !getToken('useNum')) {
			if (!options.url.includes('api/sms/getUpdPasswordCode') && !options.url.includes('api/base/forgetPassword')) {
				if (options.auth) {
					store.dispatch('showAuthModal');
				}
			}
		}
		if (options.url.includes('usrChatObjectItem/getUnReadListByEntity') == false) {
			uni.showLoading({
				title: "加载中"
			})
		}
		uni.request({
			url: options.interception ? options.url : BASE_URL + options.url,
			method: options.methods || "GET",
			data: options.data || {},
			dataType: options.dataType || "json",
			header: {
				'token': uni.getStorageSync('token'),
				'content-type': 'application/x-www-form-urlencoded',
				...options.hearder
			},
			success: (res) => {
				if ((res.data && res.data.message && res.data.message.includes('敏感词')) || (res
						.data && res.data.msg && res.data.msg.includes('敏感词'))) {
					let str = res.data.message
					uni.showToast({
						title: res.data.message || res.data.msg,
						icon: 'none'
					})
					resolve(res.data.message)
				}
				uni.hideLoading()
				if ((res.data && res.data.message && res.data.message.includes('token')) || (res.data && res.data.msg && res.data.msg.includes('token'))) {
					if (!options.url.includes('api/sms/getUpdPasswordCode') && !options.url.includes('api/base/forgetPassword')) {
						setUserInfo('')
						store.dispatch('showAuthModal');
					}
				} else {
					resolve(res)
				}
			},
			fail: (err) => {
				uni.hideLoading()
				uni.showToast({
					title: "请求失败",
					icon: 'none',
					mask: true,
				})
				reject(err)
			}
		})
	})
}
